package jeecg.business.job;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import jeecg.business.entity.alarm.AlarmWordEntity;
import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.entity.province.ProvinceEntity;
import jeecg.business.entity.setting.NmsSystemSettingEntity;
import jeecg.business.entity.site.SiteEntity;
import jeecg.business.service.alarm.AlarmWordServiceI;
import jeecg.business.service.email.NmsSendMailServiceI;
import jeecg.business.service.enter.EnterServiceI;
import jeecg.business.service.keyinfo.KeyinfoServiceI;
import jeecg.business.service.keyword.KeywordServiceI;
import jeecg.business.service.province.ProvinceServiceI;
import jeecg.business.service.setting.NmsSystemSettingServiceI;
import jeecg.business.service.site.SiteServiceI;
import jeecg.business.thread.HtmlFetchManage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class FetchTask {
	@Autowired
	KeywordServiceI keywordService;
	@Autowired
	SiteServiceI siteService;
	@Autowired
	HtmlFetchManage htmlFetchManage;
	@Autowired
	AlarmWordServiceI alarmWordService;
	@Autowired
	NmsSystemSettingServiceI settingService;
	@Autowired
	EnterServiceI enterService;
	@Autowired
	KeyinfoServiceI keyinfoService;
	@Autowired
	NmsSendMailServiceI mailService;
	@Autowired
	ProvinceServiceI provinceService;

	public static List<AlarmWordEntity> alarmList;

	public static NmsSystemSettingEntity setting;

	public static boolean running = false;
	
	public static Map<String , Boolean> runningMap = new ConcurrentHashMap<String , Boolean>();

	public static List<ProvinceEntity> provinceList;
	

	@Scheduled(cron="15 0/15 * * * ?")
//	@Scheduled(cron="20 0/1 * * * ?")
	public void each15m() {
		System.out.println("每十五分钟执行一次");
		runTask("0 0/15 * * * ?");
	}
	
	@Scheduled(cron="25 0/30 * * * ?")
//	@Scheduled(cron="40 0/1 * * * ?")
	public void each30m() {
		System.out.println("每三十分钟执行一次");
		runTask("0 0/30 * * * ?");
	}

	@Scheduled(cron="0 55 0/1 * * ?")
//	@Scheduled(cron="55 0/1 * * * ?")
	public void eachHour() {
		System.out.println("每一小时执行一次");
		runTask("0 0 0/1 * * ?");
	}
	
	@Scheduled(cron="0 10 0/3 * * ?")
	public void each3Hour() {
		System.out.println("每三小时执行一次");
		runTask("0 0 0/3 * * ?");
	}
	
	@Scheduled(cron="0 20 0/6 * * ?")
	public void each6Hour() {
		System.out.println("每六小时执行一次");
		runTask("0 0 0/6 * * ?");
	}
	
	@Scheduled(cron="0 30 0/12 * * ?")
	public void each12Hour() {
		System.out.println("每十二小时执行一次");
		runTask("0 0 0/12 * * ?");
	}
	
	@Scheduled(cron="0 35 8 0/1 * ?")
	public void each1day() {
		System.out.println("每天执行一次");
		runTask("0 0 0 0/1 * ?");
	}

	boolean intime(){
		Date date = new Date();
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		int hour = cl.get(Calendar.HOUR_OF_DAY);
//		if(hour>=7&&hour<=23){
//			return true;
//		}
//		return false;
		return true;
	}
	
	
	void runTask(String cron) {
		if (runningMap.containsKey(cron) && runningMap.get(cron)){
			System.out.println("跳过本次抓取");
			return;
		}
		try {
			if(intime()){
				provinceList = provinceService.loadAll(ProvinceEntity.class);
				htmlFetchManage.setWork();
				runningMap.put(cron, true);
//				System.out.println("running task" + htmlFetchManage);
				alarmList = alarmWordService.loadAll(AlarmWordEntity.class);
				setting = settingService.get();
				/**
				 * 企业
				 */
				List<EnterEntity> enterList = enterService.list();
				if(enterList != null && enterList.size()>0){
					for(EnterEntity enter : enterList){
						if(cron.equals(enter.getZxpl()))
							runTask(enter);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	public static String getAlarmString(){
		if(alarmList == null) return null;
		String alarmStr = null;
		for(AlarmWordEntity al : alarmList){
			if(alarmStr == null) alarmStr = al.getCy();
			else alarmStr += "|"+al.getCy();
		}
		return alarmStr;
	}
	
	void runTask(EnterEntity enter){
		List<KeywordEntity> keywordList = keywordService.find(enter.getId());
		if(keywordList != null){
			for(KeywordEntity keyword : keywordList){
				runTask(enter , keyword);
			}
		}
	}
	
	void runTask(EnterEntity enter , KeywordEntity keyword){
		List<SiteEntity> siteList = siteService.findFetch(keyword.getId());
		if (siteList != null)
			for (SiteEntity site : siteList)
				runTask(enter , keyword , site);
	}

	

	void runTask(EnterEntity enter , KeywordEntity keyword, SiteEntity site){
		System.out.println(enter.getName()+"     "+site.getMc());
		htmlFetchManage.addTask(enter , keyword, site);
	}

	
	public static void main(String[] args) {
		String k = "福建师范大学|福建福州大学|福建工程学院";
		System.out.println(k.split("\\|").length);
	}
}
